package com.gohome.gohomechat.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gohome.gohomechat.R;
import com.gohome.gohomechat.model.entify.Contant;
import com.gohome.gohomechat.model.entify.Notice;

import java.util.List;

/**
 * Created by dllo on 16/3/1.
 */
public class NoticeListViewAdapter extends BaseAdapter {
    private List<Notice> datas;
    private onClickButtonListener listener;

    public void setOnClickButtonListener(onClickButtonListener listener) {
        this.listener = listener;
    }

    public void addData(List<Notice> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.notice_listview_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
        Notice notice = datas.get(position);
        switch (notice.getMessageStatus()) {
            case Contant.MESSAGESTATUS_BEAGREED:
                holder.notice_listview_item_btn_agree.setText("已同意");
                holder.notice_listview_item_btn_agree.setEnabled(false);
                holder.notice_listview_item_btn_wait.setVisibility(View.GONE);
                break;
            case Contant.MESSAGESTATUS_BEINVITEED:
                holder.notice_listview_item_btn_agree.setText("同意");
                holder.notice_listview_item_btn_wait.setVisibility(View.GONE);
                break;
            case Contant.MESSAGESTATUS_BEREFUSED:
                holder.notice_listview_item_btn_agree.setText("被拒绝");
                holder.notice_listview_item_btn_agree.setEnabled(false);
                holder.notice_listview_item_btn_wait.setVisibility(View.GONE);
                break;
            case Contant.MESSAGESTATUS_AGREED:
                holder.notice_listview_item_btn_agree.setText("已同意");
                holder.notice_listview_item_btn_agree.setEnabled(false);
                break;
            case Contant.MESSAGESTATUS_REFUSED:
                holder.notice_listview_item_btn_agree.setText("拒绝");
                holder.notice_listview_item_btn_agree.setEnabled(false);
                break;
            default:
                holder.notice_listview_item_btn_wait.setVisibility(View.VISIBLE);
                holder.notice_listview_item_btn_agree.setVisibility(View.GONE);
                break;
        }

        holder.notice_listview_item_username.setText(notice.getUserName());
        holder.notice_listview_item_reason.setText(notice.getReason());

        holder.notice_listview_item_btn_agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.notice_listview_item_btn_agree.setText("已同意");
                holder.notice_listview_item_btn_agree.setEnabled(false);
                listener.agreeRequest(holder.notice_listview_item_username.getText().toString());
            }
        });
        return convertView;
    }

    class ViewHolder {
        private ImageView notice_listview_item_head;
        private TextView notice_listview_item_username, notice_listview_item_reason, notice_listview_item_btn_wait;
        private Button notice_listview_item_btn_agree;

        public ViewHolder(View view) {
            notice_listview_item_head = (ImageView) view.findViewById(R.id.notice_listview_item_head);
            notice_listview_item_username = (TextView) view.findViewById(R.id.notice_listview_item_username);
            notice_listview_item_btn_wait = (TextView) view.findViewById(R.id.notice_listview_item_btn_wait);
            notice_listview_item_reason = (TextView) view.findViewById(R.id.notice_listview_item_reason);
            notice_listview_item_btn_agree = (Button) view.findViewById(R.id.notice_listview_item_btn_agree);
        }
    }

    public interface onClickButtonListener {
        void agreeRequest(String userName);

        void refusedRequest(String userName);
    }
}
